import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {ElementPlusResolver} from 'unplugin-vue-components/resolvers'
import path from 'path'

function joinPath(dir) {
  return path.join(__dirname, dir)
}

// https://vite.dev/config/
export default defineConfig({

  plugins: [
    vue(),
    AutoImport({
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ['vue'],
      // 自动导入js等。注意不能直接在Dom中使用
      include: [
        /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        /\.vue$/, /\.vue\?vue/, // .vue
        /\.md$/, // .md
      ],
      resolvers: [
        ElementPlusResolver(),
      ],
      dirs: [
        'src/api', 'src/utils',
        'src/api/apiFieldMap'
      ],

      dts: 'src/autoImport/auto-import.d.ts'
    }),
    Components({
      dirs: ['src/com'],
      extensions: ['vue'],
      resolvers: [
        // 自动导入element组件
        ElementPlusResolver(),
      ],
    }),
  ],

  server: {
    host: '0.0.0.0', // 项目主机名
    https: false, // 开启https服务
    port: 314, // 项目的端口号
    proxy: { // 跨服代理服务器
      '/net/': {
        target: 'http://localhost:5000/',
        rewrite: (path) => path.replace(/^\/net/, '')
      },
    }
  },

  base: './',
  build: {
    outDir: './Build'
  },

  // 配置全局样式
  css: {
    preprocessorOptions: {
      scss: {
        javascriptEnabled: true,
        additionalData: `@use "/src/assets/css/index.scss";@use "/src/assets/css/colorList.scss";`
      }
    }
  },

  resolve: {
    alias: { // 静态文件
      '@': joinPath('src')
    }
  },


  optimizeDeps: {
    include: ['file-saver','dayjs','localeData'],
  },

})
